package cn.jiyun.controller;

import cn.jiyun.Service.Uservice;
import cn.jiyun.pojo.Day3;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.awt.print.Pageable;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UController {
    @Autowired
    private Uservice uservice;

    @GetMapping("findAll")
//    @RequestParam(required = true,defaultValue = "1")Integer pageNum
    public String findAll(Model model,Page page,String cname,Integer age,String zt){
        QueryWrapper<Day3> queryWrapper=new QueryWrapper<>();
//        LambdaQueryWrapper<Day3> queryWrapper1=new LambdaQueryWrapper<>();
//        queryWrapper1.eq(zt!=null,Day3::getZt,zt);
//        if (cname==null){
//            cname="";
//        }
//        queryWrapper.lt(age!=null && age !=0,"age",age); 小于
//        queryWrapper.eq(age!=null && age !=0,"age",age);  相等
        if (zt==null){
            queryWrapper.eq("zt",0);
        }else {
            queryWrapper.eq("zt",zt);
        }
        queryWrapper.gt(age!=null && age !=0,"age",age);
        queryWrapper.like(cname!=null,"name",cname);
        Page<Day3> pp=new Page<>(page.getCurrent(),3);
        Page<Day3> ss=uservice.fy(pp,queryWrapper);
        model.addAttribute("list",ss);
        return "ulist";
    }

    @GetMapping("del")
    public String del(Integer id) {
        uservice.del(id);
        return "redirect:findAll";
    }

    @GetMapping("tzadd")
    public String tzadd() {
        return "add";
    }



    @GetMapping("hx")
    public String hx(Integer id, Model model) {
        Day3 user = uservice.hx(id);
        model.addAttribute("user", user);
        return "update";
    }

    @PostMapping("addOrupdate")
    public String addOrupdate(Day3 day3) {
        if (day3.getId() == null) {
            uservice.add(day3);
        } else {
            uservice.update(day3);
        }
        return "redirect:findAll";
    }

}
